﻿<%@ WebHandler Language="C#" Class="ContractArchive" %>

using System;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class ContractArchive: AjaxHandlerBase
{
    //*******************************************
    //功能概要       ：基本权限初始化设定
    //参数説明       ：-
    //返回值説明     ：-
    //功能詳細       ：权限哈希表中添加操作与权限组
    //作成者         ：he.jifeng 
    //作成日         ：2010-07-05
    //*******************************************
    public ContractArchive()
    {
        //添加报备权限
        htTypePermit.Add("add", Constants.Permit_AddContractArchive);
        //更新报备权限
        htTypePermit.Add("upd", Constants.Permit_UpdContractArchive);
    }

    //*******************************************
    //功能概要       ：请求处理
    //参数説明       ：context 请求上下文
    //返回值説明     ：-
    //功能詳細       ：根据请求名 分发请求
    //作成者         ：he.jifeng
    //作成日         ：2010-07-05
    //*******************************************
    override public void RequestProcess(HttpContext context)
    {
        try
        {
            context.Response.ContentType = "text/plain";
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            
            string strOp = context.Request["op"];
            string strRet = string.Empty;
            switch (strOp.ToLower())
            {
                //添加报备相关信息
                case "add":
                    strRet = addContractArchive(context);
                    break;
                //编辑报备相关信息
                case "upd":
                    strRet = updContractArchive(context);
                    break;
                //得到预审客户信息
                case "getpreliminaryuser":
                    strRet = GetPreliminaryUser(context);
                    break;
                //得到房源信息
                case "gethousedata":
                    strRet = GetHouseData(context);
                    break;
                //得到报备信息
                case "getapplydata":
                    strRet = GetApplyData(context);
                    break;
                default:
                    break;
            }
            context.Response.Write(strRet);
        }
        catch (Exception ex)
        {
            context.Response.Write(retExecResult(Constants.ExecResult.Error, ex.Message, ""));
        }
    }
    
    //得到报备信息
    private string GetApplyData(HttpContext context)
    {
        string RecordId = context.Request["modelid"];
        StringBuilder sb = new StringBuilder();
        
        //报备信息业务类
        Chenxin.Lib.Business.BLL.ContractArchive bllContractArc = new Chenxin.Lib.Business.BLL.ContractArchive();
        //出售方信息业务类
        Chenxin.Lib.Business.BLL.VenderInfo bllVender = new Chenxin.Lib.Business.BLL.VenderInfo();
        //买方信息业务类
        Chenxin.Lib.Business.BLL.BuyerInfo bllBuyer = new Chenxin.Lib.Business.BLL.BuyerInfo();

        //获取相应房源信息
        Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo entContractArc = bllContractArc.GetModel(new Guid(RecordId));

        if (entContractArc != null)
        {
            //出售方信息
            DataSet ds_v = Chenxin.Lib.Business.DAL.t_Business_Record_SubInfo1.GetList("MainId='" + RecordId + "'");
            //买方信息
            DataSet ds_b = Chenxin.Lib.Business.DAL.t_Business_Record_SubInfo2.GetList("MainId='" + RecordId + "'");

            sb.AppendFormat("ContractArcInfo:{0},VenderInfo:{{count:{1},{2}}},BuyerInfo:{{count:{3},{4}}}", JsonHelp.Convert2Json(entContractArc), ds_v.Tables[0].Rows.Count, JsonHelp.Convert2Json(ds_v.Tables[0]), ds_b.Tables[0].Rows.Count, JsonHelp.Convert2Json(ds_b.Tables[0]));
            return string.Format("{{success:1,msg:'',data:{{{0}}}}}", sb.ToString());
        }
        else
            return "{success:0,msg:'报备信息获取失败！',data:{}";               
    }
    

    //得到房源信息
    private string GetHouseData(HttpContext context)
    {
        string HouseId = context.Request["modelid"];
        Chenxin.Lib.House.Entity.House_BaseInfo entHouseInfo = Chenxin.Lib.House.BLL.House_BaseInfo.GetHouseInfoEntity(HouseId);
        if (entHouseInfo != null)
            return string.Format("{{success:'1',msg:'成功',data:{{HouseInfo:{0}}}}}", Chenxin.Lib.Utility.JsonHelp.Convert2Json(entHouseInfo));
        else
            return "{success:0,msg:'房源信息获取失败！',data:{}";
    }
    
    

    //*******************************************
    //功能概要       ：报备添加
    //参数説明       ：context 请求上下文
    //返回值説明     ：程序处理信息提示
    //功能詳細       ：添加报备相关信息
    //作成者         ：he.jifeng
    //作成日         ：2010-07-05
    //*******************************************
    private string addContractArchive(HttpContext context)
    {
        //出售方信息
        string lsVerder = context.Request.Params["lsVerder"];
        //买方信息
        string lsBuyer = context.Request.Params["lsBuyer"];
        //签约报备信息
        string contractArc = context.Request.Params["model"];


        //出售方信息
        List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo1> lsModelOfVerder = new List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo1>();
        //买方信息
        List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo2> lsModelOfBuyer = new List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo2>();
         
        //签约报备业务类
        Chenxin.Lib.Business.BLL.ContractArchive bllContract = new Chenxin.Lib.Business.BLL.ContractArchive();
        //出售方信息业务类
        VenderInfo bllVender = new VenderInfo();
        //买方操作业务类
        BuyerInfo bllBuyer = new BuyerInfo();

        //出售方信息列表
        if (!string.IsNullOrEmpty(lsVerder))
        {
            lsModelOfVerder =
                (List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo1>)JsonHelp.Convert2Entity(lsVerder,
                        typeof(List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo1>));
        }
        //买方信息列表
        if (!string.IsNullOrEmpty(lsBuyer))
        {
            lsModelOfBuyer =
                (List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo2>)JsonHelp.Convert2Entity(lsBuyer,
                        typeof(List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo2>));
        }

        //签约报备信息
        Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo entContractArc =
            (Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo)JsonHelp.Convert2Entity(contractArc,
                    typeof(Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo));
        
       entContractArc = CompleteContract(entContractArc);
        
        //①签约报备信息录入
        bllContract.Add(entContractArc);

        //②出售方信息录入
        for (int idxCnt = 0; idxCnt < lsModelOfVerder.Count; idxCnt++)
        {
            //签约报备ID
            (lsModelOfVerder[idxCnt]).MainId = entContractArc.RecordId;
            //标识ID
            (lsModelOfVerder[idxCnt]).RecordId = Guid.NewGuid();
            bllVender.Add(lsModelOfVerder[idxCnt]);
        }
        //③买方信息录入
        for (int idxCnt = 0; idxCnt < lsModelOfBuyer.Count; idxCnt++)
        {
            //签约报备ID
            (lsModelOfBuyer[idxCnt]).MainId = entContractArc.RecordId;
            //标识ID
            (lsModelOfBuyer[idxCnt]).RecordId = Guid.NewGuid();
            if ((lsModelOfBuyer[idxCnt]).BOwner != true)
                (lsModelOfBuyer[idxCnt]).BOwner = false;
            bllBuyer.Add(lsModelOfBuyer[idxCnt]);
        }

        string text = CurrentUserInfo.UserName + "：添加签约报备。房源：[" + entContractArc.HouseCode + "]" + entContractArc.HouseAddress;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_AddContractArc"].ToString());

        return retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_ContractArchiveSucc, entContractArc.RecordId);
    }

     //*******************************************
     //功能概要       ：更新报备相关信息
     //参数説明       ：context 请求上下文
     //返回值説明     ：程序处理信息提示
     //功能詳細       ：更新报备相关信息
     //作成者         ：he.jifeng
     //作成日         ：2010-07-05
     //*******************************************
     private string updContractArchive(HttpContext context)
     {
         //出售方信息
         string lsVerder = context.Request.Params["lsVerder"];
         //买方信息
         string lsBuyer = context.Request.Params["lsBuyer"];
         //签约报备信息
         string contractArc = context.Request.Params["model"];

         //出售方信息
         List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo1> lsModelOfVerder = new List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo1>();
         //买方信息
         List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo2> lsModelOfBuyer = new List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo2>();
         
         //签约报备业务类
         Chenxin.Lib.Business.BLL.ContractArchive bllContract = new Chenxin.Lib.Business.BLL.ContractArchive();
         //出售方信息业务类
         VenderInfo bllVender = new VenderInfo();
         //买方操作业务类
         BuyerInfo bllBuyer = new BuyerInfo();

         //出售方信息列表
         if (!string.IsNullOrEmpty(lsVerder))
         {
             lsModelOfVerder =
                 (List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo1>)JsonHelp.Convert2Entity(lsVerder,
                         typeof(List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo1>));
         }
         //买方信息列表
         if (!string.IsNullOrEmpty(lsBuyer))
         {
             lsModelOfBuyer =
                 (List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo2>)JsonHelp.Convert2Entity(lsBuyer,
                         typeof(List<Chenxin.Lib.Business.Entity.t_Business_Record_SubInfo2>));
         }

         //签约报备信息
         Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo entContractArc =
             (Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo)JsonHelp.Convert2Entity(contractArc,
                     typeof(Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo));

         entContractArc = CompleteContract(entContractArc);
         
         //①签约报备信息更新
         bllContract.Update(entContractArc);

         //②删除出售方信息
         bllVender.DeleteByMainId(entContractArc.RecordId);
         
         //③出售方信息录入
         for (int idxCnt = 0; idxCnt < lsModelOfVerder.Count; idxCnt++)
         {
             //签约报备ID
             (lsModelOfVerder[idxCnt]).MainId = entContractArc.RecordId;
             //标识ID
             (lsModelOfVerder[idxCnt]).RecordId = Guid.NewGuid();
             bllVender.Add(lsModelOfVerder[idxCnt]);
         }
         
         //④删除买方信息
         bllBuyer.DeleteByMainId(entContractArc.RecordId);
         
         //⑤买方信息录入
         for (int idxCnt = 0; idxCnt < lsModelOfBuyer.Count; idxCnt++)
         {
             //签约报备ID
             (lsModelOfBuyer[idxCnt]).MainId = entContractArc.RecordId;
             //标识ID
             (lsModelOfBuyer[idxCnt]).RecordId = Guid.NewGuid();
             if ((lsModelOfBuyer[idxCnt]).BOwner != true)
                 (lsModelOfBuyer[idxCnt]).BOwner = false;
             bllBuyer.Add(lsModelOfBuyer[idxCnt]);
         }

         string text = CurrentUserInfo.UserName + "：编辑签约报备。房源：[" + entContractArc.HouseCode + "]" + entContractArc.HouseAddress ;
         AddLog(text, BusinessType.BusinessTypeComboList()["OprType_UpdContractArc"].ToString());

         return retExecResult(Constants.ExecResult.Succ, Resources.Message.Msg_ContractArchiveSucc, entContractArc.RecordId);
     }
     //*******************************************
     //功能概要       ：填充报备信息字段
     //参数説明       ：context 请求上下文
     //返回值説明     ：报备信息实体
     //功能詳細       ：填充报备信息字段
     //作成者         ：he.jifeng
     //作成日         ：2010-07-05
     //*******************************************
     private Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo CompleteContract(Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo entContractArc)
     {
         //添加报备信息
         if (entContractArc.RecordId.Equals(Guid.Empty))
         {
             entContractArc.RecordId = Guid.NewGuid();
             entContractArc.CreateCityId = CurrentUserInfo.CityId;
             entContractArc.CreateCityName = CurrentUserInfo.CityName;
             entContractArc.CreateCompanyId = CurrentUserInfo.CompanyId;
             entContractArc.CreateCompanyName = CurrentUserInfo.CompanyName;
             entContractArc.CreateDepartmentId = CurrentUserInfo.DepartId;
             entContractArc.CreateDepartmentName = CurrentUserInfo.DepartName;
             entContractArc.CreateTime = DateTime.Now;
             entContractArc.CreateUserId = CurrentUserInfo.UserId;
             entContractArc.CreateUserName = CurrentUserInfo.UserName;
             entContractArc.Status = 0;
             entContractArc.AnswersNum = 0;
         }
         //更新报备信息
         else
         {
             //签约报备信息处理业务类
             Chenxin.Lib.Business.BLL.ContractArchive bllContract = new Chenxin.Lib.Business.BLL.ContractArchive();
             Chenxin.Lib.Business.Entity.t_Business_Record_BaseInfo entOldContractArc = bllContract.GetModel(entContractArc.RecordId);

             entContractArc.CreateCityId = entOldContractArc.CreateCityId;
             entContractArc.CreateCityName = entOldContractArc.CreateCityName;
             entContractArc.CreateCompanyId = entOldContractArc.CreateCompanyId;
             entContractArc.CreateCompanyName = entOldContractArc.CreateCompanyName;
             entContractArc.CreateDepartmentId = entOldContractArc.CreateDepartmentId;
             entContractArc.CreateDepartmentName = entOldContractArc.CreateDepartmentName;
             entContractArc.CreateTime = entOldContractArc.CreateTime;
             entContractArc.CreateUserId = entOldContractArc.CreateUserId;
             entContractArc.CreateUserName = entOldContractArc.CreateUserName;

             entContractArc.BelongUserCityId = entOldContractArc.BelongUserCityId;
             entContractArc.BelongUserCityName = entOldContractArc.BelongUserCityName;
             entContractArc.BelongUserCompanyId = entOldContractArc.BelongUserCompanyId;
             entContractArc.BelongUserCompanyName = entOldContractArc.BelongUserCompanyName;
             entContractArc.BelongUserDepartmentId = entOldContractArc.BelongUserDepartmentId;
             entContractArc.BelongUserDepartmentName = entOldContractArc.BelongUserDepartmentName;
             entContractArc.BelongUserId = entOldContractArc.BelongUserId;
             entContractArc.BelongUserName = entOldContractArc.BelongUserName;

             entContractArc.ApplyDocAnswerContent = entOldContractArc.ApplyDocAnswerContent;
             entContractArc.ApplyDocIsAnswer = entOldContractArc.ApplyDocIsAnswer;
             entContractArc.LoanAnswerContent = entOldContractArc.LoanAnswerContent;
             entContractArc.LoanIsAnswer = entOldContractArc.LoanIsAnswer;
             entContractArc.LawAnswerContent = entOldContractArc.LawAnswerContent;
             entContractArc.LawIsAnswer = entOldContractArc.LawIsAnswer;
             entContractArc.AnswersNum = entOldContractArc.AnswersNum;
             entContractArc.IsAnswer = entOldContractArc.IsAnswer;
             entContractArc.AnswerTime = entOldContractArc.AnswerTime;
             entContractArc.Status = entOldContractArc.Status;
             entContractArc.EvaluatePrice = entOldContractArc.EvaluatePrice;
             entContractArc.LonggevityLevel = entOldContractArc.LonggevityLevel;
             entContractArc.LoanBank = entOldContractArc.LoanBank;
             entContractArc.EvaluateCompany = entOldContractArc.EvaluateCompany;
             entContractArc.FundLoanPrice = entOldContractArc.FundLoanPrice;
             entContractArc.FundLoanEndYear = entOldContractArc.FundLoanEndYear;
             entContractArc.BusinessLoanPrice = entOldContractArc.BusinessLoanPrice;
             entContractArc.BusinessLoanEndYear = entOldContractArc.BusinessLoanEndYear;
             entContractArc.Prejudication = entOldContractArc.Prejudication;
         }

         entContractArc.CityId = CurrentUserInfo.CityId;
         entContractArc.CityName = CurrentUserInfo.CityName;
         entContractArc.CompanyId = CurrentUserInfo.CompanyId;
         entContractArc.CompanyName = CurrentUserInfo.CompanyName;
         
         entContractArc.UpdateCityId = CurrentUserInfo.CityId;
         entContractArc.UpdateCityName = CurrentUserInfo.CityName;
         entContractArc.UpdateCompanyId = CurrentUserInfo.CompanyId;
         entContractArc.UpdateCompanyName = CurrentUserInfo.CompanyName;
         entContractArc.UpdateDepartmentId = CurrentUserInfo.DepartId;
         entContractArc.UpdateDepartmentName = CurrentUserInfo.DepartName;
         entContractArc.UpdateTime = DateTime.Now;
         entContractArc.UpdateUserId = CurrentUserInfo.UserId;
         entContractArc.UpdateUserName = CurrentUserInfo.UserName;

         return entContractArc;
     }

     private string GetPreliminaryUser(HttpContext context)
     {
         System.Text.StringBuilder strRet = new StringBuilder();
         string HouseId = context.Request.Params["HouseId"];
         //预审下家业务类
         PreBuyerInfo bllBuyer = new PreBuyerInfo();
         //预审上家业务类
         PreliminaryBaseInfo bllSaler = new PreliminaryBaseInfo();

         Chenxin.Lib.Business.Entity.t_Preliminary_BaseInfo entPreSaler = bllSaler.GetLatestModelByHouseId(HouseId);
         DataSet ds_buy = bllBuyer.GetLatestListByHouseId(HouseId);

         strRet.Append("{");
         strRet.Append("success:'1',");
         strRet.Append("msg:'',");
         strRet.AppendFormat("data:{{PreSalerInfo:{0},PreBuyerInfo:{{count:{1},{2}}}}}", JsonHelp.Convert2Json(entPreSaler), ds_buy.Tables[0].Rows.Count, JsonHelp.Convert2Json(ds_buy.Tables[0]));
         strRet.Append("}");

         return strRet.ToString();
     }
}